package com.huluobo.a2206parkdemo.visitor

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.DatePicker
import android.widget.Toast
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2206parkdemo.R
import com.huluobo.a2206parkdemo.databinding.ActivityAddVisitorBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.Calendar

@AndroidEntryPoint
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding, VisitorViewModel>() {
    private var startTime: Int = 0
    private var endTime: Int = 0
    override fun initData() {
        vm.addVisitorSuccess.observe(this) {
            ToastUtils.showLong("添加来访成功")
            finish()
        }
    }

    override fun initView() {
        binding.addVisitorStartTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                { _, year, month, dayOfMonth ->
                    binding.addVisitorStartTimeNowTv.text = "$year-${month + 1}-$dayOfMonth"
                    startTime = dayOfMonth
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addVisitorEndTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                { _, year, month, dayOfMonth ->
                    binding.addVisitorEndTimeNowTv.text = "$year-${month + 1}-$dayOfMonth"
                    endTime = dayOfMonth
                    val tampTime = endTime - startTime
                    binding.addVisitorTimeNowTv.text = "${tampTime}天"
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addVisitorCommitBtn.setOnClickListener {

            val carNum = binding.addVisitorCarNumNowEt.text.toString()
            val desc = binding.addVisitorDescNowEt.text.toString()

            if (binding.addVisitorStartTimeNowTv.text.toString().isNotEmpty() &&
                binding.addVisitorEndTimeNowTv.text.toString().isNotEmpty() &&
                binding.addVisitorTimeNowTv.text.toString().isNotEmpty() &&
                carNum.isNotEmpty() && desc.isNotEmpty()
            ) {
                val map = mutableMapOf<String, String>()
                map[Const.PARAM_VISITOR_START] = binding.addVisitorStartTimeNowTv.text.toString()
                map[Const.PARAM_VISITOR_END] = binding.addVisitorEndTimeNowTv.text.toString()
                map[Const.PARAM_VISITOR_TIME] = binding.addVisitorTimeNowTv.text.toString()
                map[Const.PARAM_VISITOR_CAR_NUM] = carNum
                map[Const.PARAM_VISITOR_DESC] = desc

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)

                vm.addVisitor(body)
            } else {
                ToastUtils.showLong("来访详情不能有空值,请检查")
            }
        }
    }
}